const Common_User_Service = require("../sercice/CommonUserSevire.js")
const md5 = require("md5")
const script = require("../config/config").script
const JWT = require("../utils/token")
const Common_user_control = {
    add: async (req, res) => {

        try {
            const { username, password } = req.body
            const result = await Common_User_Service.add_user({ username, password: md5(password + script) }, password)
            console.log(result)
            res.status(200).send({
                msg: "成功",
                code: 200
            })
        } catch (error) {
            res.status(412).send({
                msg: "error",
                code: 412
            })
        }
    },
    get_list: async (req, res) => {
        try {
            const result = await Common_User_Service.getList(req.params.id)
            res.send({
                msg: "获取成功",
                data: result.data,
                count: result.count,
                code: 200
            })
        } catch (error) {
            res.send({
                "msg": "失败"
            })
        }
    },
    put_user: async (req, res) => {
        try {
            // 51a1f324b59fe5a46fc6633692751de7
            const { _id, username, cryptogram, role } = req.body
            console.log(md5(cryptogram, script), cryptogram, '修改')
            const result = await Common_User_Service.put_user(
                {
                    _id,
                    username,
                    password: md5(cryptogram + script),
                    cryptogram,
                    role
                }
            )
            if (result.matchedCount === 0) {
                res.status(411).send({
                    msg: "修改失败",
                    code: 411
                })
            } else {
                res.status(200).send({
                    msg: "修改成功",
                    data: result,
                    code: 200
                })
            }

        } catch (error) {
            res.status(411).send({
                msg: "修改失败",
                code: 411
            })
        }
    },
    delete_user: async (req, res) => {
        try {
            const result = await Common_User_Service.delete_user(req.body._id)
            console.log(result)
            console.log(req.body._id)
            res.send({
                msg: "删除成功",
                code: 200
            })
        } catch (error) {
            res.status(411).send({
                msg: "删除成功",
                code: 411
            })
        }
    },
    login_user: async (req, res) => {
        try {
            const { username, password } = req.body
            let token = JWT.crateJWT({ username, password })
            res.header("Access-Control-Expose-Headers", 'Token')
            res.header("Token", token)
            console.log(password, md5(password, script), '登录')
            const result = await Common_User_Service.login({ username, password: md5(password + script) })
            if (result) {
                res.status(200).send({
                    msg: "登陆成功",
                    data: result,
                    code: 200,
                    token: token
                })
            } else {
                res.status(400).send({
                    msg: "登录失败",
                    data: {},
                    code: 400
                })
            }

        } catch (error) {
            res.status(400).send({
                msg: "登录失败",
                data: {},
                code: 400
            })
        }
    },
    upload_avatar: async (req, res) => {

        try {
            const result = await Common_User_Service.user_upload(req.body.id, req.body.url)
            console.log(result)
            res.status(200).send({
                msg: "修改成功",
                code: 200
            })
        } catch (error) {
            res.status(411).send({
                msg: "修改失败",
                code: 411
            })
        }
    }
}



module.exports = Common_user_control